package com.zx.dsptuneup_TriMic.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.base.tools.tools.StringUtils;
import com.zx.dsptuneup_TriMic.Base.BaseAdapter;
import com.zx.dsptuneup_TriMic.R;
import com.zx.dsptuneup_TriMic.bean.DeviceBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ReplaceParentAdapter extends BaseAdapter {
    private Context context;
    private List<DeviceBean> gridViewBeanList;
    private OnReplaceParentClickListener onReplaceParentClickListener;

    public List<DeviceBean> getGridViewBeanList() {
        return gridViewBeanList;
    }

    public void setGridViewBeanList(List<DeviceBean> gridViewBeanList) {
        this.gridViewBeanList = gridViewBeanList;
        notifyDataSetChanged();
    }

    public void setOnReplaceClickListener(OnReplaceParentClickListener onReplaceParentClickListener) {
        this.onReplaceParentClickListener = onReplaceParentClickListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_replace_parent_device,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final ViewHolder viewHolder = (ViewHolder) holder;
        DeviceBean childDeviceBean = gridViewBeanList.get(position);
        viewHolder.main_title.setText(StringUtils.isEmpty(childDeviceBean.getMac()) ? "未知mac地址" : "mac: "+childDeviceBean.getMac());
        viewHolder.sub_title.setText(StringUtils.isEmpty(childDeviceBean.getIp()) ? "未知ip地址": "ip  : "+childDeviceBean.getIp());
        viewHolder.replace_do.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onReplaceParentClickListener != null){
                    onReplaceParentClickListener.OnReplaceParentClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return gridViewBeanList.size();
    }

    public ReplaceParentAdapter(Context context, List<DeviceBean> gridViewBeanList) {
        super(context);
        this.context = context;
        this.gridViewBeanList = gridViewBeanList;
    }

    @Override
    public void notifyDataSetChanged(List dataList) {

    }

    /**
     * 替换点击回调方法
     */
    public interface OnReplaceParentClickListener {
        void OnReplaceParentClick(int position);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.main_title)
        TextView main_title;
        @BindView(R.id.sub_title)
        TextView sub_title;
        @BindView(R.id.replace_do)
        TextView replace_do;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

    }
}
